# @Time : 2025/2/27 11:38
# @Author : Fioman 
# @Phone : 13149920693
"""
根据文本生成带下划线的方便背诵的文本
"""
import os


def process_line(line, mode):
    """处理单行文本，根据模式生成不同下划线替换版本，保留空格"""
    line = line.rstrip('\n')  # 去除换行符
    processed = []
    replace_next = (mode == 1)  # 模式1：初始保留字符，模式2：初始替换字符
    for char in line:
        if char == ' ':  # 保留空格
            processed.append(' ' * 3)
        else:
            if replace_next:
                processed.append(char)  # 保留字符
            else:
                processed.append('_')  # 替换为下划线
            replace_next = not replace_next  # 切换替换状态
    return ''.join(processed) + '\n'  # 恢复换行符


# 遍历当前文件夹下的所有文件
current_dir = os.getcwd()
for filename in os.listdir(current_dir):
    # 筛选原始文本文件，排除已生成的结果文件
    if filename.endswith('.txt') and not ('_res_01' in filename or '_res_02' in filename):
        base, ext = os.path.splitext(filename)
        input_path = os.path.join(current_dir, filename)

        # 生成两个输出文件的路径
        output_path_01 = os.path.join(current_dir, f"{base}_res_01{ext}")
        output_path_02 = os.path.join(current_dir, f"{base}_res_02{ext}")

        # 读取原始文件内容
        with open(input_path, 'r', encoding='utf-8') as f_in:
            lines = f_in.readlines()

        # 处理每一行并生成两种版本
        processed_01 = [process_line(line, 1) for line in lines if line is not None]
        processed_02 = [process_line(line, 2) for line in lines if line is not None]

        # 写入结果文件
        with open(output_path_01, 'w', encoding='utf-8', newline='\r\n') as f_out1:
            f_out1.writelines(processed_01)
        with open(output_path_02, 'w', encoding='utf-8', newline='\r\n') as f_out2:
            f_out2.writelines(processed_02)

print("文件处理完成！")
